/**
 * 
 * @param {Object} "ui.combo"
 * @param {Object} {options:{width:'auto'
 * @param {Object} panelWidth:null
 * @param {Object} panelHeight:200
 * @param {Object} editable:true
 * @param {Object} disabled:false
 * @param {Object} required:false
 * @param {Object} topTools:[]
 * @param {Object} bottomTools:[]
 * @param {Object} selectPrev:function(
 */
(function($,undefined){
	$.widget("ui.combo",{
		options:{
			width:'auto',
			panelWidth:null,
			panelHeight:200,
			editable:true,
			disabled:false,
			required:false,
			topTools:[],
			bottomTools:[],
			selectPrev:function(){},
			selectNext:function(){},
			filter:false,
			onChange:function(newValue,oldValue){}
		},
		_create:function(){
			this.element.hide();
			var _self = this;
			this.comboSpan = $("<span class='buss-combo'/>").insertAfter(this.element);
			var input = $("<input class='buss-combo-text' type='text'/>").appendTo(this.comboSpan);
			$("<span><span class='buss-combo-arrow'></span></span>").appendTo(this.comboSpan);
			$("<input type='hidden' class='buss-combo-value'/>").appendTo(this.comboSpan);
			this.comboPanel = $("<div class='buss-combo-panel' style='position:absolute;display:block;'/>");
			var items = $("<ul style='display:block;' class='buss-combo-itempanel'></ul>");
			this.comboPanel.append(items);		
					
		},
		_init:function(){			
			var _self = this;
			var _width = this.element.outerWidth();
			if(!this.options.editable){
				this.comboSpan.find(".buss-combo-text").attr("readonly","readonly");
			}
			if(this.options.width != 'auto'){
				_width = this.options.width;
			}
			this.comboSpan.find(".buss-combo-text").css("width",_width - this.comboSpan.find(".buss-combo-arrow").outerWidth());
			var _panelWidth = 0;
			if(this.options.panelWidth == null){
				_panelWidth = _width + 2;
			}else{
				_panelWidth = this.options.panelWidth + 2;
			}
			
			this.comboPanel.panel({
				width:_panelWidth,
				height:_self.options.panelHeight,
				topTools:_self.options.topTools,
				bottomTools:_self.options.bottomTools,
				onRestore:function(){
					//alert("tt");
					var curVal = _self.comboSpan.find(".buss-combo-value").val();
					var curItem = _self.comboPanel.find(".buss-panel-body-content li[value="+curVal+"]");
					_self.comboPanel.find(".buss-panel-body-content ul li").removeClass("buss-combo-panel-itemsel");
					if(curItem.length > 0){
						curItem.addClass("buss-combo-panel-itemsel");
						var h = _self.comboPanel.find(".buss-panel-body-content").scrollTop() + curItem.position().top;
						h -= (curItem.outerHeight() +  _self.comboPanel.find(".buss-panel-body .buss-panel-top-toolbar").outerHeight() + _self.comboPanel.find(".buss-panel-body .buss-panel-bottom-toolbar").outerHeight());
					}
							
					_self.comboPanel.find(".buss-panel-body-content").scrollTop(h);
								
					var ulHeight = $(this).find("ul").outerHeight();
					var panelHeight = $(this).panel("option","height");
					var ctxHeight = panelHeight - _self.comboPanel.find(".buss-panel-body .buss-panel-top-toolbar").outerHeight() + _self.comboPanel.find(".buss-panel-body .buss-panel-bottom-toolbar").height();
					if(ulHeight < ctxHeight){
						$(this).panel("setBodyHeight",ulHeight);
					}else{
						$(this).panel("setBodyHeight",ctxHeight - 5);
					}
					var zIndex = parseInt(_self.comboPanel.css('z-index'));
					zIndex = isNaN(zIndex) ? 0 : zIndex;
					var maxZIndex = $.maxZIndex();
					if (maxZIndex == 0 || maxZIndex > zIndex){
						_self.comboPanel.css("z-index",maxZIndex + 1);
					}					
					_self._setPosition();
				}
			});	
			this.comboPanel.find(".buss-panel-body").addClass("buss-combo-panel-topborder");
			this.element.unbind("_destroy.buss");
			this.element.bind("_destroy.buss",function(){
				_self.comboPanel.remove();
			})
			
			this.comboPanel.panel("close");
			var zIndex = parseInt(this.comboPanel.css('z-index'));
			zIndex = isNaN(zIndex) ? 0 : zIndex;
			var maxZIndex = $.maxZIndex();
			if (maxZIndex == 0 || maxZIndex > zIndex){
				this.comboPanel.css("z-index",maxZIndex + 1);
			}			
			this.comboPanel.find(".buss-panel-body").css("border","1px solid #879faf");
			
			if (_self.options.topTools.length > 0){
				this.comboPanel.find(".buss-panel-body").css("border-top","none");
			}
			this.comboPanel.find(".buss-panel-bottom-toolbar").css("border-top","none");
			this._setPosition();
			this._bindEvents();		
			this.refresh();
		},
		refresh:function(){
			var _self = this;
			_self.comboPanel.find(".buss-panel-body-content ul").empty();
			var curSelItem = this.element.find("option[selected]");
			_self.comboSpan.find(".buss-combo-value").val(curSelItem.val());
			_self.comboSpan.find(".buss-combo-text").val(curSelItem.text());			
			_self.comboPanel.find(".buss-panel-body-content ul")
			
			this.element.children().each(function(i,o){
				var val = $(this).val();
				var html = $(this).html();
				//multiple
				var li = $("<li value='"+val+"'>"+html+"</li>");
				li.click(function(){					
					_self._changeValue($(this));					
					_self.comboPanel.panel("close");
				})
				_self.comboPanel.find(".buss-panel-body-content ul").append(li);
			})
			
		},
		_changeValue:function(curItem){			
			var oldValues = this.comboSpan.find(".buss-combo-value").val();
			this.comboSpan.find(".buss-combo-value").val(curItem.attr('value'));
			this.comboSpan.find(".buss-combo-text").val(curItem.html());
			this.element.val(curItem.attr('value'));					
			this.options.onChange.call(this.element, curItem.attr('value'), oldValues);
		},
		changeValue:function(val){
			var oldValues = this.comboSpan.find(".buss-combo-value").val();
			this.comboSpan.find(".buss-combo-value").val(val);
			var text = this.element.find("option[value='"+val+"']").html();
			this.comboSpan.find(".buss-combo-text").val(text);
			this.element.val(val);					
			this.options.onChange.call(this.element, val, oldValues);
		},
		_setPosition:function(){
			var inputOffset = this.comboSpan.offset();
			var bottomGap = $(window).height() - inputOffset.top + this.comboSpan.outerHeight();
			
			if(inputOffset.top >= this.comboPanel.outerHeight() && bottomGap <= this.comboPanel.outerHeight()){
				this.options._pos = "top";
				
				var top = inputOffset.top - this.comboPanel.outerHeight() + 3;
				
				var left = inputOffset.left;		
				
				this.comboPanel.css({top:top,left:left});
				this.comboPanel.find(".buss-panel-top-toolbar").css("border-top","1px solid #B2C8D0");
			}else{
				this.options._pos = "bottom";
				var top = inputOffset.top + this.comboSpan.height() + 2;
				var left = inputOffset.left;		
				this.comboPanel.css({top:top,left:left});
			}		
				
		},
		_selectPrev:function(){
			
			var curItem = this.comboPanel.find(".buss-panel-body-content ul .buss-combo-panel-itemsel:visible");
			if(curItem.length == 0){
				curItem = this.comboPanel.find(".buss-panel-body-content ul li:visible:last");
				curItem.addClass("buss-combo-panel-itemsel");	
			}else if(curItem.prevAll(":visible").length > 0) {
				curItem.prevAll(":visible:first").addClass("buss-combo-panel-itemsel");
				curItem.removeClass("buss-combo-panel-itemsel");
			}
			curItem = this.comboPanel.find(".buss-panel-body ul .buss-combo-panel-itemsel");
			this._changeValue(curItem);

			var panel = this.comboPanel.find(".buss-panel-body-content");
			var top_toolbar = this.comboPanel.find(".buss-panel-body .buss-panel-top-toolbar");
			if(curItem.length > 0 && curItem.position().top - top_toolbar.outerHeight() < 0){
				var h = panel.scrollTop() + curItem.position().top - top_toolbar.outerHeight() ;
				panel.scrollTop(h);
			}		
		},
		_selectNext:function(){			
			var curItem = this.comboPanel.find(".buss-panel-body ul .buss-combo-panel-itemsel");	
			if(curItem.length == 0){
				curItem = this.comboPanel.find(".buss-panel-body ul li:visible:first");
				curItem.addClass("buss-combo-panel-itemsel");	
			}else if(curItem.nextAll(":visible").length > 0){
				curItem.removeClass("buss-combo-panel-itemsel");
				curItem.nextAll(":visible:first").addClass("buss-combo-panel-itemsel");
			}	
			curItem = this.comboPanel.find(".buss-panel-body ul .buss-combo-panel-itemsel");
			this._changeValue(curItem);
			
			var panel = this.comboPanel.find(".buss-panel-body-content");
			var top_toolbar = this.comboPanel.find(".buss-panel-body .buss-panel-top-toolbar");
			var bottom_toolbar = this.comboPanel.find(".buss-panel-body .buss-panel-bottom-toolbar");			
			 if(curItem.length > 0 && curItem.position().top  >= panel.height()){
				var h = panel.scrollTop() + curItem.position().top + curItem.outerHeight() - panel.outerHeight();
				h -= top_toolbar.outerHeight();
				panel.scrollTop(h);
			}			
		},
		getIndexValue:function(index){
			var selItem = this.comboPanel.find(".buss-panel-body-content ul li").eq(index);
			if (selItem.length ==0){
				return null;
			}
			return {value:selItem.attr('value'),text:selItem.text()}			
			
		},
		getValue:function(value){
			var selItem = this.comboPanel.find(".buss-panel-body-content li[value="+value+"]");
			if (selItem.length ==0){
				return null;
			}
			
			return {value:selItem.attr('value'),text:selItem.text()}
		},
		setIndexValue:function(index,text){
			var selItem = this.comboPanel.find(".buss-panel-body-content ul li").eq(index);
			if(selItem.length == 0){
				return false;
			}
			var option = this.element.find("option").eq(index);
			option.html(text);
			selItem.text(text);
			return true; 
		},
		setValue:function(value,text){
			var selItem = this.comboPanel.find(".buss-panel-body-content li[value="+value+"]");
			if(selItem.length == 0){
				return false;
			}
			var option = this.element.find("option[value="+value+"]");
			option.html(text);
			selItem.text(text);
			return true; 
		},
		addItem:function(value,text){
			_self = this;
			var item = $("<li/>").attr("value",value).text(text);
			this.comboPanel.find(".buss-panel-body-content ul").append(item);
			var option = $("<option/>").val(value).html(text);
			this.element.append(option);
			this._bindEvents();
			_self._changeValue($(this));
			item.click(function(){					
				_self._changeValue($(this));					
				_self.comboPanel.panel("close");
			})
		},
		filter:function(){	
			var panel = this.comboPanel;
			var value = this.comboSpan.find(".buss-combo-text").val();
			var filterItem = panel.find(".buss-panel-body-content ul li:not(:contains('"+value+"'))");
			panel.find(".buss-panel-body-content ul li").removeClass("buss-combo-panel-itemsel").show();
			filterItem.hide();
			var ulHeight = panel.find("ul").height();
			var panelHeight = panel.panel("option","height");
			var ctxHeight = panelHeight - panel.find(".buss-panel-body .buss-panel-top-toolbar").outerHeight() + panel.find(".buss-panel-body .buss-panel-bottom-toolbar").outerHeight();
			if(ulHeight < ctxHeight){
				panel.panel("setBodyHeight",ulHeight);
				if (this.options._pos == "top"){
					//this.comboPanel.css({top:top,left:left});
					panel.css({top:panel.offset().top + (ctxHeight - ulHeight)});
					//alert(panel.offset().top);
				}
			}else{				
				panel.panel("setBodyHeight",ctxHeight);
				if (this.options._pos == "top"){
					var top = this.comboSpan.offset().top - panel.height() + 3;		
					panel.css({top:top});
				}
			}
		},
		_bindEvents:function(){
			var _self = this;
			$("dobument").unbind(".combo");
			_self.comboPanel.unbind(".combo");
			_self.comboSpan.find(".buss-combo-text").unbind(".combo");
			_self.comboSpan.find(".buss-combo-arrow").unbind(".combo");
			$(document).bind("mousedown.combo",function(e){						
				_self.comboPanel.panel("close");
			})
			_self.comboPanel.bind('mousedown.combo',function(e){
				e.stopPropagation();
				return false;
			})
			
			_self.comboSpan.find(".buss-combo-text").bind("focus.combo",function(e){				
					
				_self.comboPanel.panel("restore");
			}).bind("mousedown.combo",function(e){			
				e.stopPropagation();		
			}).bind('keyup.combo',function(e){
				switch(e.keyCode){
					case $.ui.keyCode.LEFT:
					case $.ui.keyCode.UP:
						if (_self.comboPanel.panel("option",'minimized') == true){
							_self.comboPanel.panel("restore");
							return;
						};
						_self._selectPrev();
						_self.options.selectPrev.call(_self.comboPanel);
						break;
					case $.ui.keyCode.RIGHT:
					case $.ui.keyCode.DOWN:
						if (_self.comboPanel.panel("option",'minimized') == true){
							_self.comboPanel.panel("restore");
							return;
						}
						_self._selectNext();
						_self.options.selectNext.call(_self.comboPanel);
						break;
					case $.ui.keyCode.ENTER:
						var curSelItem = _self.comboPanel.find("ul .combo-panel-itemsel");						
						if(curSelItem.length > 0){
							_self.element.val(curSelItem.attr('value'));
							_self.comboSpan.find(".buss-combo-value").val(curSelItem.attr('value'));							
							_self.comboSpan.find(".buss-combo-text").val(curSelItem.text());	
						}
						_self.comboPanel.panel("close");
						break;
					case $.ui.keyCode.ESCAPE:
						_self.comboPanel.panel("close");
						break;					
					default:
						if(_self.options.filter == true){
							_self.filter();
						}
				}
			}).bind("keydown.combo",function(e){
				switch (e.keyCode) {
					case $.ui.keyCode.TAB:
						_self.comboPanel.panel("close");
						break;
				}
			})
			_self.comboSpan.find(".buss-combo-arrow").bind("click.combo",function(){
				_self.comboSpan.find(".buss-combo-text").focus();
			});
			
		}
	})
})(jQuery);
